﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using LibHuman;


namespace Human
{
    public partial class frmcnThueTN : Form
    {
        string m_thang = "", m_nam = "",s_mmyy="",sql="";
        AccessData acc = new AccessData();
        DataSet ds = new DataSet();
        public frmcnThueTN(string thang,string nam)
        {
            InitializeComponent();
            m_thang = thang;
            m_nam = nam;
            s_mmyy = m_thang + m_nam.Substring(2, 2);
        }
        string abc;
        private void button1_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void txtthap_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
                SendKeys.Send("{Tab}");
        }

        private void txtCao_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
                SendKeys.Send("{Tab}");
        }

        private void txtTyle_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
                SendKeys.Send("{Tab}");
        }

        private void frmcnThueTN_Load(object sender, EventArgs e)
        {
            txtID.Hide();
            load_grid();
        }
        private void load_grid()
        {
            sql = "select * from " + acc.user + ".tvn where mmyy='" + s_mmyy + "'";
            ds = acc.get_data(sql);
            dataGridView1.DataSource = ds.Tables[0];
            CurrencyManager cm = (CurrencyManager) BindingContext[dataGridView1.DataSource, dataGridView1.DataMember];
            DataView dv = (DataView)cm.List;
            dv.AllowNew = false;
            dv.AllowEdit = false;
            dv.AllowDelete = false;
        }

        private void cmdThem_Click(object sender, EventArgs e)
        {
            int m_stt = 0,stt_pre=0;
            DataSet ds1 = new DataSet();
            sql = "select max(id) from " + acc.user + ".tvn where mmyy='"+s_mmyy+"'";
            try
            {
                m_stt = int.Parse( acc.get_data(sql).Tables[0].Rows[0][0].ToString())+1;
            }
            catch
            {
                m_stt = 1;
            }
            txtID.Text = m_stt.ToString();
            switch (m_stt)
            {
                case 1:
                 
                    txtthap.Text = "0";
                   break;
                default :
                    stt_pre = m_stt - 1;
                    sql = "select * from " + acc.user + ".tvn where mmyy='" + s_mmyy + "' and id="+stt_pre;
                    ds1 = acc.get_data(sql);
                    foreach (DataRow r in ds1.Tables[0].Rows)
                    {
                        txtthap.Text = decimal.Parse(r["gh_tren"].ToString()).ToString("#,##0.00");
                    }
                    break;
            }
            txtCao.Text = "";
            txtTyle.Text = "";
            txtCao.Focus();
            enable_ctl(true);
        }
        void enable_ctl(bool che)
        {
          
            txtCao.Enabled = che;
            txtTyle.Enabled = che;
            dataGridView1.Enabled = !che;
            cmdThem.Enabled = !che;
            cmdHuy.Enabled = !che;
            buttonsave.Enabled = che;
            buttonundo.Enabled = che;
            cmdThoat.Enabled = !che;

        }

        private void txtthap_Validated(object sender, EventArgs e)
        {
            decimal so = 0;
            if (txtthap.Text.Trim() != "")
            {
                try
                {
                    so = decimal.Parse(txtthap.Text.Trim());
                    txtthap.Text = so.ToString("###,###");
                }
                catch
                {
                    MessageBox.Show("Nhập số !",AccessData.Msg, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    txtthap.Focus();
                    return;
                }
            }
            else
                txtthap.Text = "0";
        }

        private void txtCao_Validated(object sender, EventArgs e)
        {
            decimal so = 0;
            if (txtCao.Text.Trim() != "")
            {
                try
                {
                    so = decimal.Parse(txtCao.Text.Trim());
                    txtCao.Text = so.ToString("###,###");
                }
                catch
                {
                    MessageBox.Show("Nhập số !",AccessData.Msg, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    txtCao.Focus();
                    return;
                }
            }
            else
                txtCao.Text = "0";
        }

        private void txtTyle_Validated(object sender, EventArgs e)
        {
            decimal so = 0;
            if (txtTyle.Text.Trim() != "")
            {
                try
                {
                    so = decimal.Parse(txtTyle.Text.Trim());
                    txtTyle.Text = so.ToString("###,###");
                }
                catch
                {
                    MessageBox.Show("Nhập số !", AccessData.Msg, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    txtTyle.Focus();
                    return;
                }
            }
            else
                txtTyle.Text = "0";
        }

        private void buttonsave_Click(object sender, EventArgs e)
        {
            int m_id=0;
            decimal thap=0,cao=0,tyle=0;
            if (txtthap.Text != "")
                thap = decimal.Parse(txtthap.Text);
            if (txtCao.Text != "")
                cao = decimal.Parse(txtCao.Text);
            if (txtTyle.Text != "")
                tyle = decimal.Parse(txtTyle.Text);
            if (txtID.Text != "")
                m_id = int.Parse( txtID.Text);
            acc.upd_thueTNV(m_id, thap, cao, tyle, s_mmyy);
            load_grid();
            enable_ctl(false);

        }

        private void buttonundo_Click(object sender, EventArgs e)
        {
            txtthap.Text = "";
            txtCao.Text = "";
            txtTyle.Text = "";
            load_grid();
            enable_ctl(false);
        }

        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                DataRowView arv = (DataRowView)(dataGridView1.CurrentRow.DataBoundItem);
                txtID.Text = arv["id"].ToString();
                txtthap.Text = (arv["gh_duoi"].ToString()!=""?decimal.Parse(arv["gh_duoi"].ToString()).ToString("#,##0.00"):"0");
                txtCao.Text = (arv["gh_tren"].ToString() != "" ? decimal.Parse(arv["gh_tren"].ToString()).ToString("#,##0.00") : "0");
                txtTyle.Text = (arv["tyle_thue"].ToString() != "" ? decimal.Parse(arv["tyle_thue"].ToString()).ToString("#,##0.00") : "0");
            }
            catch
            {
            }
        }

        private void dataGridView1_CurrentCellChanged(object sender, EventArgs e)
        {
            dataGridView1_CellClick(null, null);
        }

        private void cmdHuy_Click(object sender, EventArgs e)
        {
            if (txtID.Text != "")
            {
                if (MessageBox.Show("Ban có thật sự muốn hủy không ?",AccessData.Msg, MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    sql = "delete from " + acc.user + ".tvn where mmyy='" + s_mmyy + "' and id=" + txtID.Text;
                    acc.Execute_data(sql);
                    load_grid();
                }
            }
        }

        private void txtCao_KeyPress(object sender, KeyPressEventArgs e)
        {
            if ((e.KeyChar < '0' || e.KeyChar > '9') && !(e.KeyChar == '\b' || e.KeyChar == '.' || e.KeyChar == ','))
                e.KeyChar = '\0';
        }
    }
}